package com.online.shop.service.impl;


import com.online.shop.common.SverResponse;
import com.online.shop.dao.ActionOrderDao;
import com.online.shop.dao.ActionUserDao;
import com.online.shop.pojo.User;
import com.online.shop.service.ActionUserService;
import com.online.shop.utils.ConstUtil;
import com.online.shop.utils.MD5Util;
import com.online.shop.utils.TokenCache;
import com.online.shop.vo.ActionUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class ActionUserServiceImpl implements ActionUserService {
	@Autowired
	private ActionUserDao actionUserDao;
	@Autowired
	private ActionOrderDao orderDao;
	@Override
	public SverResponse<User> doLogin(String account, String password) {
		// TODO Auto-generated method stub
		//1.判断用户名是否存在
		int rs=actionUserDao.checkUserByAccount(account);
		if(rs==0){
			return SverResponse.createByErrorMessage("用户不存在");
		}
		//2根据用户名和密码查找用户
		String md5pwd= MD5Util.MD5Encode(password, "utf-8", false);
		//User user=actionUserDao.findUserByAccountAndPassword(account, password);
		
		User user=actionUserDao.findUserByAccountAndPassword(account, md5pwd);
		//3.判断查找的用户是否存在
		if(user==null){
			return SverResponse.createByErrorMessage("密码错误");
		}
		//置空密码
		user.setPassword(StringUtils.EMPTY);
		return SverResponse.createRespBySuccess("登录成功",user);
		//男生版本return SverResponse.createRespBySuccessMessage("登录成功");
	}
	

/*	@Override
	public SverResponse<String> isAdmin(User user) {
		// TODO Auto-generated method stub
		if(user.getRole()==ConstUtil.Role.ROLE_ADMIN){
			return SverResponse.createRespBySuccess();
		}
		return SverResponse.createRespByError();
	}*/
	
	/*@Override
	public SverResponse<List<ActionUserVo>> findUserList() {
		// TODO Auto-generated method stub
		List<ActionUserVo> vos=Lists.newArrayList();
		
		//1.调用dao层类的方法
		List<User> users=actionUserDao.findAllUsers();
		//2.处理：user对象转换成ActionUserVo对象
		for(User u:users){
			vos.add(setNormalProperty(u));
		}
		return SverResponse.createRespBySuccess(vos);
	}
	//将User转换为ActionUserVo对象
	private ActionUserVo setNormalProperty(User user){
		ActionUserVo vo=new ActionUserVo();
		vo.setAccount(user.getAccount());
		vo.setAge(user.getAge());
		vo.setEmail(user.getEmail());
		vo.setId(user.getId());
		vo.setName(user.getName());
		vo.setPhone(user.getPhone());
		if(user.getSex()==1){
			vo.setSex("男");
		}
		else {
			vo.setSex("女");
		}
		return vo;
	}


	@Override
	public SverResponse<ActionUserVo> findUser(Integer id) {
		// TODO Auto-generated method stub
		//1.调用dao层的类中的方法
		User user=actionUserDao.findUserById(id);
		//2.将user对象装换成ActionUserVo对象
		ActionUserVo vo=setNormalProperty(user);
		return SverResponse.createRespBySuccess(vo);
	}*/


	@Override
	public SverResponse<User> updateUserInfo(ActionUserVo userVo) {
		// TODO Auto-generated method stub
		//1.根据id获得user对象
		User user=actionUserDao.findUserById(userVo.getId());
		//2.把userVo修改的属性值赋值给user对象
		user.setAccount(userVo.getAccount());
		user.setAge(userVo.getAge());
		user.setEmail(userVo.getEmail());
		user.setName(userVo.getName());
		user.setPhone(userVo.getPhone());
		if(userVo.getSex().equals("男")){
			user.setSex(1);
		}else{
			user.setSex(0);
		}
		user.setUpdate_time(new Date());
	
		//3.调用dao层的方法
		int rs=actionUserDao.updateUserInfo(user);
		if(rs>0){
			return SverResponse.createRespBySuccess("用户信息修改成功!",user);
		}
		return SverResponse.createByErrorMessage("用户信息修改失败！");
	}


	/*@Override
	public SverResponse<String> delUsers(Integer id) {
		// TODO Auto-generated method stub
		//1.判断用户是否有订单，如果没有订单则删除
		if(orderDao.findOrderByUid(id).size()>0){
			return SverResponse.createByErrorMessage("用户存在关联的订单，无法删除！");
		}
		//2.删除用户，实际是修改用户的del状态字段
		User user=actionUserDao.findUserById(id);
		user.setDel(1);
		user.setUpdate_time(new Date());
		int rs=actionUserDao.updateUserInfo(user);
		if(rs>0){
			return SverResponse.createRespBySuccess("用户删除成功！");
		}
		return SverResponse.createByErrorMessage("用户信息修改失败！");
	}*/


	@Override
	public SverResponse<String> doRegister(User user) {
		// TODO Auto-generated method stub
		//1.检查用户名是否存在
		SverResponse<String> resp= checkValidation(user.getAccount(), ConstUtil.TYPE_ACCOUNT);
		if(!resp.isSuccess()){
			return resp;
		}
		//2.检查邮箱是否被注册
		resp= checkValidation(user.getEmail(),ConstUtil.TYPE_EMAIL);
		if(!resp.isSuccess()){
			return resp;
		}
		//3.指定用户角色，通过前端注册的用户都为客户
		user.setRole(ConstUtil.Role.ROLE_CUSTOMER);
		//4.密码加密
		user.setPassword(MD5Util.MD5Encode(user.getPassword(), "utf-8", false));
		//5.执行注册
		Date curDate = new Date();
		user.setCreate_time(curDate);
		user.setUpdate_time(curDate);
		int rs =actionUserDao.insertUser(user);
		if(rs==0){
			return SverResponse.createByErrorMessage("注册失败!");
		}
		return SverResponse.createRespBySuccessMessage("注册成功");
	}


	@Override
	public SverResponse<String> checkValidation(String str, String type) {
		// TODO Auto-generated method stub
		//判断字符串长度不为空
		if(StringUtils.isNotBlank(type)){
			if(ConstUtil.TYPE_ACCOUNT.equals(type)){
				int rs=actionUserDao.checkUserByAccount(str);
				if(rs > 0){
					return SverResponse.createByErrorMessage("用户名已经存在");
				}
			}
			if(StringUtils.isNotBlank(type)){
				if(ConstUtil.TYPE_EMAIL.equals(type)){
					int rs=actionUserDao.checkUserByEmail(str);
					if(rs > 0){
						return SverResponse.createByErrorMessage("Eamil已经存在");
					}
				}
			}
			if(StringUtils.isNotBlank(type)){
				if(ConstUtil.TYPE_PHONE.equals(type)){
					int rs=actionUserDao.checkUserByPhone(str);
					if(rs > 0){
						return SverResponse.createByErrorMessage("电话号码已经存在");
					}
				}
			}
			
		}else {
			return SverResponse.createByErrorMessage("信息类别错误");
		}
		return SverResponse.createRespBySuccessMessage("信息验证成功");
	}


	@Override
	public SverResponse<User> findUserByAccount(String account) {
		// 通过用户名查找到用户
		User user=actionUserDao.findUserByAccount(account);
		if(user==null){
			return SverResponse.createByErrorMessage("用户名错误!");
		}
		//将密码置空
		user.setPassword(StringUtils.EMPTY);
		//将安全问题答案置空
		user.setAsw(StringUtils.EMPTY);
		return SverResponse.createRespBySuccess(user);
	}


	@Override
	public SverResponse<String> checkUserAnswer(String account, String question, String asw) {
		// TODO Auto-generated method stub
		//获取校验结果
		int rs=actionUserDao.checkUserAnswer(account,question,asw);
		if(rs > 0){
			//答案正确,生成token
			String token=UUID.randomUUID().toString();
			//放入缓存
			TokenCache.setCacheData(TokenCache.PREFIX+account, token);
			return SverResponse.createRespBySuccessMessage(token);
		}
		return SverResponse.createByErrorMessage("问题答案错误!");
	}


	@Override
	public SverResponse<String> resetPassword(Integer userId, String password) {
		// TODO Auto-generated method stub
		
		//密码加密
		String pwd = MD5Util.MD5Encode(password, "utf-8", false);
		//获得user对象
		User user=actionUserDao.findUserById(userId);
		//更新密码
		user.setPassword(pwd);
		user.setUpdate_time(new Date());
		int rs=actionUserDao.updateUserInfo(user);
		if(rs>0){
			return SverResponse.createRespBySuccessMessage("密码修改成功！");
		}
		return SverResponse.createByErrorMessage("密码修改失败");
	}


	@Override
	public SverResponse<String> updatePassword(User user, String newPassword, String oldPassword) {
		//检测旧密码是否正确
		oldPassword=MD5Util.MD5Encode(oldPassword, "utf-8", false);
		int rs=actionUserDao.checkPassword(user.getAccount(),oldPassword);
		if(rs==0){
			return SverResponse.createByErrorMessage("原始密码错误！");
		}
		
		//将新密码插入数据库
		newPassword=MD5Util.MD5Encode(newPassword, "utf-8", false);
		user.setPassword(newPassword);
		user.setUpdate_time(new Date());
		rs=actionUserDao.updateUserInfo(user);
		if(rs>0){
			return SverResponse.createRespBySuccessMessage("密码修改成功!");
		}
		return SverResponse.createByErrorMessage("密码修改失败！");
	}


	
	
	
	
}
